import { getDetails, getDetailsByPhone } from '@/api/admin/user';
import { USER_TYPE_DIC, USER_TYPE } from '@/const/type';
import { checkPwdStren } from '@/util';
import { PWD_STRENGTH } from '@/const/type';
import { validatenull } from '@/util/validate';
var validateUsername = (rule, value, callback) => {
  const flag = new RegExp(/^([a-z\u4e00-\u9fa5\d]+?)$/).test(value);
  if (!flag) {
    callback(new Error('用户名支持小写英文、数字、中文'));
  }

  getDetails(value).then((response) => {
    if (window.boxType === 'edit') callback();
    const result = response.data.data;
    if (result !== null) {
      callback(new Error('用户名已经存在'));
    } else {
      callback();
    }
  });
};

var validatePhone = (rule, value, callback) => {
  getDetailsByPhone(value).then((response) => {
    if (window.boxType === 'edit') callback();
    const result = response.data.data;
    if (result !== null) {
      callback(new Error('手机号已经存在'));
    } else {
      callback();
    }
  });
};
export const tableOption = () => {
  return {
    ...window.$crudMixins.tableOptions,
    viewBtn: true,
    editBtn: false,
    delBtn: false,
    addBtn: false,
    menuWidth: 120,
    labelWidth: 120,
    column: [
      {
        fixed: true,
        label: 'id',
        prop: 'userId',
        span: 24,
        hide: true,
        editDisabled: true,
        addDisplay: false,
      },
      {
        fixed: true,
        label: '用户名',
        prop: 'username',
        editDisabled: true,
        search: true,
        span: 24,
        rules: [
          {
            required: true,
            message: '请输入用户名',
          },
          {
            min: 3,
            max: 20,
            message: '长度在 3 到 20 个字符',
            trigger: 'blur',
          },
          { validator: validateUsername, trigger: 'blur' },
        ],
      },
      {
        label: '密码',
        prop: 'password',
        type: 'password',
        hide: true,
        span: 24,
        viewDisplay: false,
        component: 'PwdTipIndex',
        maxlength: 20,
        rules: [
          {
            required: true,
            validator: (rule, value, callback) => {
              const pwdLen = checkPwdStren(value);
              if (value === '******') {
                callback();
                return;
              }
              if (validatenull(value)) {
                callback(new Error('请输入密码'));
              } else if (pwdLen < PWD_STRENGTH.STRONG) {
                // 请输入6位以上强度为强的密码！
                callback(new Error('请输入6位以上强度为强的密码！'));
              } else {
                callback();
              }
            },
            trigger: 'blur',
          },
        ],
      },
      {
        label: '姓名',
        prop: 'name',
        value: '',
        span: 24,
        width: 100,
        rules: [
          {
            min: 2,
            max: 64,
            required: true,
            message: '请输入姓名',
            trigger: 'blur',
          },
        ],
      },
      {
        label: '手机号',
        prop: 'phone',
        value: '',
        span: 24,
        width: 120,
        rules: [
          {
            min: 11,
            max: 11,
            required: true,
            message: '长度在 11 个字符',
            trigger: 'blur',
          },
          { validator: validatePhone, trigger: 'blur' },
        ],
      },
      {
        label: '类型',
        prop: 'type',
        type: 'radio',
        value: '0',
        width: 100,
        dicData: USER_TYPE_DIC,
        rules: [
          {
            required: true,
            message: '请选择状态',
            trigger: 'blur',
          },
        ],
        control: (val, form) => {
          return {
            baseIds: {
              display: val === USER_TYPE.BASE,
            },
          };
        },
      },
      {
        label: '所属爱心收容站',
        prop: 'baseIds',
        span: 24,
        display: false,
        hide: true,
        dicUrl: '/core/base/list',
        type: 'select',
        multiple: true,
        props: {
          label: 'name',
          value: 'id',
        },
        rules: [
          {
            required: true,
            message: '请选择所属爱心收容站',
            trigger: 'change',
          },
        ],
      },
      // {
      //   label: '岗位',
      //   prop: 'post',
      //   overHidden: true,
      //   formslot: true,
      //   slot: true,
      //   span: 24,
      //   rules: [
      //     {
      //       required: true,
      //       message: '请选择岗位',
      //       trigger: 'blur',
      //     },
      //   ],
      // },
      {
        label: '角色',
        prop: 'role',
        overHidden: true,
        span: 24,
        width: 200,
        dicUrl: '/admin/role/list',
        type: 'select',
        multiple: true,
        props: {
          label: 'roleName',
          value: 'roleId',
        },
        rules: [
          {
            required: true,
            message: '请选择角色',
            trigger: 'blur',
          },
        ],
      },
      {
        label: '状态',
        prop: 'lockFlag',
        type: 'radio',
        slot: true,
        border: true,
        span: 24,
        width: 100,
        rules: [
          {
            required: true,
            message: '请选择状态',
            trigger: 'blur',
          },
        ],
        value: '0',
        dicData: [
          {
            label: '有效',
            value: '0',
          },
          {
            label: '锁定',
            value: '9',
          },
        ],
      },
      {
        hide: true,
        label: '昵称',
        prop: 'nickname',
        value: '',
        span: 24,
        rules: [
          {
            min: 1,
            max: 64,
            message: '请输入昵称',
            trigger: 'blur',
          },
        ],
      },
      {
        hide: true,
        label: '邮箱',
        prop: 'email',
        value: '',
        span: 24,
        rules: [
          {
            min: 4,
            max: 128,
            message: '请输入邮箱',
            trigger: 'blur',
          },
        ],
      },
      {
        label: '创建时间',
        prop: 'createTime',
        type: 'datetime',
        format: 'yyyy-MM-dd HH:mm',
        valueFormat: 'yyyy-MM-dd HH:mm:ss',
        editDisabled: true,
        addDisplay: false,
        span: 24,
        width: 150,
      },
    ],
  };
};
